package org.itboys.store.weixin;

import com.google.common.collect.Lists;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.entity.FuzionMember;
import org.itboys.store.entity.CarItem;
import org.itboys.store.service.StoreCarItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by huml on 2016/5/19.
 * 微网购物车
 */
@RestController
@RequestMapping("/store/weixin/carItem")
public class StoreCarItemWeixinController extends BaseController {

    @Autowired
    private StoreCarItemService carItemService;

    /**
     * 我的购物车
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/carItemList")
    public ModelAndView carItemList(HttpServletRequest request,
                                    @RequestParam("type")Integer type,
                                    Model model){
        FuzionMember member = (FuzionMember) request.getSession().getAttribute("member");
        List<CarItem> list = carItemService.getMemberCars(member.getId(), type);
        model.addAttribute("cars", list);
        return new ModelAndView("site/carItem");
    }

    /**
     * 添加产品到购物车
     * @param productId
     * @param request
     * @param num
     * @param response
     */
    @RequestMapping("/addCarItem")
    public void addCarItem(@RequestParam("productId")Long productId,
                           HttpServletRequest request,
                           @RequestParam("num")Integer num,
                           HttpServletResponse response){
        try {
            FuzionMember member = (FuzionMember) request.getSession().getAttribute("member");
            CarItem item = carItemService.addCarItem(member.getId(), productId, num);
            response.sendRedirect("/store/weixin/carItem/carItemList?type="+item.getType());
        } catch (Exception e) {
            logger.error("/weixin/carItem/addCarItem error", e);
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }

    /**
     * 修改购物车中产品的数量
     * @param carId
     * @param num
     * @param response
     */
    @RequestMapping("/updateNum")
    public void updateNum(@RequestParam("carId")Long carId,
                          @RequestParam("num")Integer num,
                          HttpServletResponse response){
        try {
            carItemService.updateNum(carId, num);
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(0, CommonConstants.SUCCESS));
        } catch (Exception e) {
            logger.error("/weixin/carItem/updateNum error", e);
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }

    /**
     * 删除购物车中的产品
     * @param carId
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(@RequestParam("carId")Long carId,
                       HttpServletResponse response){
        try {
            carItemService.delete(Lists.newArrayList(carId));
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(0, CommonConstants.SUCCESS));
        } catch (Exception e) {
            logger.error("/weixin/carItem/updateNum error", e);
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }

}
